package com.jt.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import java.util.List;

/**
 * @author Cx
 * @stage JT Of Fourth stage
 * @creat 2020-12-12 10:34
 */
@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JedisCluster jedisCluster;

    @RequestMapping("check/{param}/{type}")
    public JSONPObject checkUser(
            @PathVariable String param,
            @PathVariable Integer type,
            String callback) {
        boolean flag = userService.checkUser(param, type);
        SysResult sysResult = SysResult.success(flag);
        return new JSONPObject(callback, sysResult);
    }

    @RequestMapping("findUserList")
    public List<User> findUserList() {
        List<User> userList = userService.findUserList();
        return userList;
    }

    /**
     * 跨域请求:完成用户信息获取
     * URL网址:  http://sso.jt.com/user/query/dca70b16a1c54aea9ebb0b27621250de?callback=jsonp1608021961735&_=1608021961777
     * 参数:    参数1: ticket信息    参数2:callback
     * 返回值:   SysResult对象(用户数据......)
     */
    @RequestMapping("query/{ticket}")
    public JSONPObject findUserByTicket(@PathVariable String ticket,
                                        String callback) {
        // 从redis中获取数据
        if (jedisCluster.exists(ticket)) {
            String userJSON = jedisCluster.get(ticket);
            SysResult sysResult = SysResult.success(userJSON);
            return new JSONPObject(callback, sysResult);
        } else {
            return new JSONPObject(callback, SysResult.fail());
        }
    }

}
